package cn.com.hhrcw.enterprise.web;

import cn.com.hhrcw.entity.OpDeliveryHistory;
import cn.com.hhrcw.entity.OpEntryHistory;
import cn.com.hhrcw.entity.PiInfo;
import cn.com.hhrcw.service.IOpDeliveryHistoryService;
import cn.com.hhrcw.service.IOpEntryHistoryService;
import cn.com.hhrcw.service.IPiInfoService;
import cn.com.hhrcw.sms.service.ISysSmsTemplateService;
import cn.com.hhrcw.utils.EmailUtil;
import cn.com.hhrcw.utils.NewsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 职位投递记录
 *
 * @program: recruit-boot-parent
 * @author: JD
 * @create: 2020-11-10 19:24
 **/
@RestController
@RequestMapping("/en/index")
@Slf4j
public class ScheduleController {

    @Autowired
    IOpDeliveryHistoryService deliveryHistoryService;

    @Autowired
    private ISysSmsTemplateService templateService;

    @Autowired
    private IOpEntryHistoryService entryHistoryService;

    @Autowired
    private NewsUtil newsUtil;

    @Autowired
    private EmailUtil emailUtil;

    @Autowired
    private IPiInfoService piInfoService;

    @Autowired
    private ISysSmsTemplateService service;

    /**
     * 根据投递记录id修改求职者状态
     *
     * @param dataId 投递记录id
     * @param state  0 未阅读  1 已阅读 2 不合适 3通过筛选4 面试 5已发offer(已录用) 6 已入职 7 被系统过滤 8未入职9反馈面试结果(通过)10反馈面试结果(不通过)
     * @param type   不合适类型 不合适原因
     *               0:工作经验不满足要求数量
     *               1:技术不符合岗位要求
     *               2:薪资不匹配
     *               3:学历不匹配
     *               4:行业不匹配
     *               5:招聘类型不匹配
     * @return
     */
    @GetMapping("/editInfoById")
    public Result editState(String dataId, Integer state, String type, Boolean emailFlag) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<OpDeliveryHistory> list = new ArrayList<>();
        String conStr = ",";
        if (dataId.contains(conStr)) {
            String[] ids = dataId.split(conStr);
            list = deliveryHistoryService.lambdaQuery().in(OpDeliveryHistory::getId, ids).list();
        } else {
            OpDeliveryHistory history = deliveryHistoryService.getById(dataId);
            list.add(history);
        }
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(history -> {
                PiInfo piInfo = piInfoService.lambdaQuery().eq(PiInfo::getUserId, history.getUserId()).one();
                Integer stateOld = history.getState();
                history.setState(state);
                if (StringUtils.isNotBlank(type)) {
                    history.setInappropriateInfo(type);
                }
                if (state != null) {
                    if (state == 1) {
                        if (stateOld == null || stateOld < 1) {
                            deliveryHistoryService.updateById(history);
                        }
                    } else {
                        deliveryHistoryService.updateById(history);
                    }
                    boolean flag = state == 2;
                    boolean flagTwo = state == 6;
                    boolean flagThree = state == 8;
                    if (flag) {
                        Map<String, String> map = emailUtil.creatEmail(history.getUserId(), sysUser.getId(), null, null, history.getId(), "8");
                        String title = map.get("tittle");
                        String content = map.get("content");
                        //短信通知不合适
                        templateService.sendSms(new String[]{piInfo.getPhone()}, "【" + title + "】" + content);
                        //系统消息
                        newsUtil.creatNews(history.getUserId(), sysUser.getId(), 0, 1, history.getId(), "8");

                        if (emailFlag != null && emailFlag) {
                            service.sendEmail(content, title, (File) null, piInfo.getEmail());
                        }
                    } else if (state == 1) {
                    } else if (flagTwo) {
                        OpEntryHistory entryHistory = entryHistoryService.lambdaQuery().eq(OpEntryHistory::getUserId, history.getUserId())
                                .eq(OpEntryHistory::getDelFlag, false).eq(OpEntryHistory::getJobId, history.getJobId()).one();
                        entryHistory.setEntryFlag(true);
                        entryHistoryService.updateById(entryHistory);
                    } else if (flagThree) {//标未入职
                        OpEntryHistory entryHistory = entryHistoryService.lambdaQuery().eq(OpEntryHistory::getUserId, history.getUserId())
                                .eq(OpEntryHistory::getDelFlag, false).eq(OpEntryHistory::getJobId, history.getJobId()).one();
                        entryHistory.setEntryFlag(false);
                        entryHistoryService.updateById(entryHistory);
                    }
                }

            });
        }
        return Result.ok();
    }

}
